package com.ocvdemo;

import static com.ocvdemo.TestUtils.centerOfRect;
import static com.ocvdemo.TestUtils.createGreyImage;
import static com.ocvdemo.TestUtils.detect;
import static com.ocvdemo.TestUtils.readImage;
import static com.ocvdemo.TestUtils.showImage;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

public class TestOpenCV {
	
	private static final String eyeClassifer = "\\src\\main\\resources\\classifier\\haarcascade_eye.xml";
	private static final String mouthClassifer = "\\src\\main\\resources\\classifier\\haarcascade_mcs_mouth.xml";

	private static final String leftEyeClassifer = "\\src\\main\\resources\\classifier\\haarcascade_mcs_lefteye.xml";
	private static final String rightEyeClassifer = "\\src\\main\\resources\\classifier\\haarcascade_mcs_righteye.xml";
	private static final String noseClassifer = "\\src\\main\\resources\\classifier\\haarcascade_mcs_nose.xml";
	
	private static final String imgDesc = "\\src\\main\\resources\\img\\lena.jpg";
	
	private static final Scalar GREEN = new Scalar(0, 255, 0);
	private static final Scalar RED = new Scalar(255, 0, 0);
	private static final Scalar BLUE = new Scalar(0, 0, 255);
	private static final Scalar WHITE = new Scalar(255, 255, 255);
	
	private Mat image , grey , edge , cedge;
	
	TestOpenCV() {
		cedge = new Mat();
		edge = new Mat();
	}
	
	public static void main(String[] args) {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);		
		TestOpenCV test = new TestOpenCV();
		test.image = readImage(imgDesc);		
//		test.applyCannyDetector(test.image);
		
		// detect features
		DetectedFeature feature = test.detectFeaures();
		
		// Connect features 
		Core.line(test.image, feature.leftEyeCords, feature.rightEyeCords, WHITE);
		Core.line(test.image, feature.leftEyeCords, feature.noseCords, WHITE);
		Core.line(test.image, feature.rightEyeCords, feature.noseCords, WHITE);
		
		showImage(test.image);
	}
	
	public DetectedFeature detectFeaures() {
		
		DetectedFeature feature = new DetectedFeature();
		
		MatOfRect matOfRect = detect(image,eyeClassifer,imgDesc,GREEN);		
		Rect[] rects = matOfRect.toArray();		
		feature.setLeftEyeCords(centerOfRect(rects[0])); 
				
//		matOfRect = detect(image,rightEyeClassifer,imgDesc,BLUE);
//		rects = matOfRect.toArray();
		feature.setRightEyeCords(centerOfRect(rects[1]));
		
		matOfRect = detect(image,noseClassifer,imgDesc,RED);
		rects = matOfRect.toArray();
		feature.setNoseCords(centerOfRect(rects[0]));
		
		return feature;
	}

	public Mat applyCannyDetector(Mat image) {		
		cedge.create(image.size(), image.type());		
		grey = createGreyImage(image);
	    Imgproc.cvtColor(image, grey, Imgproc.COLOR_BGR2GRAY);
	    Imgproc.blur(grey,edge,new Size(3,3));
	    Imgproc.Canny(edge, edge, 2.0, 5.0,5,true);
	    cedge.setTo(Scalar.all(0.0));
	    image.copyTo(cedge, edge);
	    return cedge;
	}


}
